package com.lhl.aop.aspectj;

import com.lhl.aop.annotation.Secured;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author king
 */
@Aspect
@Slf4j
public class SecuredMethodAspect {

    @Before("@annotation(com.lhl.aop.annotation.Secured)")
    public void beforePrint(JoinPoint joinPoint) {
        log.debug("----Before----");
    }

    @Pointcut("@annotation(secured)")
    public void callAt(Secured secured) {
        log.debug("---------------");
    }

    @Around("callAt(secured)")
    public Object around(ProceedingJoinPoint pjp, Secured secured) throws Throwable {
        if (secured.isLocked()) {
            log.info(pjp.getSignature().toLongString() + " is locked");
            return null;
        } else {
            return pjp.proceed();
        }
    }
}
